import { AlertController, ModalController } from '@ionic/angular';
import { Component, Input, OnInit } from '@angular/core';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { VideoImgPopupComponent } from '../video-img-popup/video-img-popup.component';
import { EventService } from 'src/app/shared/service/event.service';
@Component({
  selector: 'app-designated-order-receiver',
  templateUrl: './designated-order-receiver.component.html',
  styleUrls: ['./designated-order-receiver.component.scss'],
})
export class DesignatedOrderReceiverComponent implements OnInit {
  @Input() tagData: any;
  emptyDataHint = "暂无数据";
  showLoadMore = false;
  pageNo = 1;
  pageSize: number;
  pageTotal: number;
  // 筛选及页面数据参数
  params: any = { content: {} };
  listenerName: any;

  constructor(
    private pageModalService: PageModalService,
    private appGlobalService: AppGlobalService,
    public alertController: AlertController,
    private httpService: HttpService,
    public modalController: ModalController,
    public eventService: EventService,
  ) { }

  ngOnInit() {
    this.listenerName = this.tagData.name + 'AskForList';
    this.eventService.eventEmit.on(this.listenerName, (obj) => {
      this.refreshListData(obj);
    });
    this.pageTotal = this.tagData.pageTotal;
    this.pageNo = this.tagData.pageNo;
    this.pageSize = this.tagData.pageSize;
    // tslint:disable-next-line: max-line-length
    this.showLoadMore = !this.tagData.data.moreButton && this.tagData.data.submit && this.pageTotal && (this.pageNo < Math.ceil(this.pageTotal / this.pageSize));
    if (this.tagData.hint !== undefined) {
      this.emptyDataHint = this.tagData.hint;
    }
  }
  go(item, e) {
    if (e) {
      e.stopPropagation();
    }
    if (item.link && item.link.link && item.link.link.type === 'phone') {
      this.presentAlertConfirm(Number(item.link.link.params.mobile), item.link.link.params.userName);
    } else if (item.link && item.link.link && item.link.link.type === 'chat') {
      this.pageModalService.openPage(item.link.link);
    } else {
      if (item.link.link) {
        this.pageModalService.openPage(item.link.link);
      } else {
        this.pageModalService.openPage(item.link);
      }
    }
  }

  async presentAlertConfirm(mobile, name?) {
    const msg = (name ? name + ' ' : '') + mobile;
    const alert = await this.alertController.create({
      header: this.tagData.nickName,
      message: msg,
      buttons: [
        {
          text: "取消",
          role: "cancel",
          cssClass: "secondary"
        },
        {
          text: "拨打",
          handler: () => {
            this.httpService.changeCallNumber(mobile);
          }
        }
      ]
    });

    await alert.present();
  }


  loadMoreData(event) {
    try {
      this.params = this.tagData.data.submit;
      this.pageNo = this.pageNo + 1;
      // // 参数
      this.params.content.pageNo = this.pageNo;
      this.params.id = this.tagData.data.submit.id;
      // 请求获取新数据
      this.params.sessionId = localStorage.getItem("sessionId");
      this.httpService.post('application/submit', this.params, res => {

        // 请求数据成功
        if (res.success) {
          if (res.data.submit) {
            this.params.content = res.data.submit.content; // 为了每次刷新下一页的参数 "oldIndex"
          }
          const resList = res.data.data;
          // 追加返回数据
          if (resList && resList.length > 0) {
            // 追加数据
            resList.map(v => {
              this.tagData.data.data.push(v);
            });
          } else {
            event.target.disabled = true;
          }
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          // this.appGlobalService.showToast(res.message, 2000, 'middle');
        }
        event.target.complete();

      }, err => {
        // 请求失败，更新当前页数
        this.pageNo = this.pageNo - 1;
        // 请求数据失败
        // this.appGlobalService.showToast(err.message, 2000, 'middle');
        event.target.complete();

      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }
  }

  refreshListData(param) {
    const that = this;
    if (param === undefined) {
      return;
    }
    this.pageNo = 1;
    this.params = { content: {} };
    this.params.content = param;
    this.params.id = this.tagData.data.submit.id;
    this.params.content.pageNo = this.pageNo;
    this.params.sessionId = localStorage.getItem("sessionId");
    this.httpService.post('application/submit', this.params, res => {
      if (res.success) {
        if (res.data.submit) {
          this.params = res.submit; // 为了每次刷新下一页的参数 "oldIndex"
        }
        this.tagData.data.data = [];
        const result = res.data.data; // JSON.parse(res.data.data);
        this.tagData.data.data = result;
        if (res.data.moreButton) {
          this.tagData.data.moreButton = res.data.moreButton;
        }
        if (res.data.submit && res.data.submit.content && res.data.submit.content.pageTotal) {
          this.pageTotal = res.data.submit.content.pageTotal;
          this.tagData.data.submit.content = res.data.submit.content;
          this.pageNo = 1;
        } else {
          this.pageTotal = this.tagData.pageTotal;
        }

        // tslint:disable-next-line: max-line-length
        this.showLoadMore = !this.tagData.data.moreButton && this.tagData.data.submit && this.pageTotal && (this.pageNo < Math.ceil(this.pageTotal / this.pageSize)) && this.tagData.asyncShow !== 'true';

      } else {
        // 请求数据失败
        this.tagData.data.data = [];
        // this.appGlobalService.showToast(res.message, 2000, 'middle');
      }
    },
      err => {
        // 请求数据失败
        this.tagData.data.data = [];
        // this.appGlobalService.showToast(err.message, 2000, 'middle');
      });
  }
}
